package org.testis.webautomationmodule.services;

import org.testis.data.TestStepDataObject;
import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleService;
import org.testis.services.AtofModuleTask;
import org.testis.utils.OsInfo;
import org.testis.utils.TerminalApplicationExecutor;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WebAutomationModuleTask extends AtofModuleTask {

    public WebAutomationModuleTask(TestStepDataObject dataObj,
            AtofModuleService service) {
        super(dataObj,
                service);
    }

    private static void closeWebBrowserForWindows() {
        if (OsInfo.isWindows()) {
            int checkCount = 4;
            TerminalApplicationExecutor tsk = new TerminalApplicationExecutor("tasklist");
            try {
                TerminalApplicationExecutor arp = new TerminalApplicationExecutor("arp -d");
                arp.runExec();
            } catch (IOException ex) {
            } catch (InterruptedException ex) {
            }
            TerminalApplicationExecutor firefoxKill = new TerminalApplicationExecutor("taskkill /F /IM  firefox.exe /T");
            while (checkCount > 0) {
                try {
                    tsk.runExec();
                } catch (IOException ex) {
                } catch (InterruptedException ex) {
                } catch (Exception ex) {
                }
                if (tsk.getOutputString().contains("firefox")) {
                    try {
                        firefoxKill.runExec();
                        Thread.sleep(2000);
                    } catch (IOException ex) {
                    } catch (InterruptedException ex) {
                    } catch (Exception ex) {
                    }
                }
                try {
                    tsk.runExec();
                } catch (IOException ex) {
                } catch (InterruptedException ex) {
                } catch (Exception ex) {
                }
                if (!tsk.getOutputString().contains("firefox")) {
                    break;
                }
                checkCount--;
            }
        }
    }

    @Override
    public void run() {
        boolean isStarted = false;
        Exception currentException = null;
        closeWebBrowserForWindows();
        try {
            WebAutomationModuleProviderService webAutomationService =
                    (WebAutomationModuleProviderService) m_service;
            webAutomationService.startAutomation();
            isStarted = true;
        } catch (TestStepException ex) {
            m_result.setResultType(ex.getResultType());
            currentException = ex;
        } catch (Exception ex) {
            m_result.setResultType(TestStepResultType.FAILURE);
            currentException = ex;
        }
        if (currentException != null) {
            try {
                LogSenderService.putMessageOnQueue("AtofModuleTask",
                        AtofLogLevel.ERROR,
                        currentException.getMessage(),
                        m_service.getDataObj().
                        getMessageId());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        if (isStarted) {
            super.run();
        }
    }

    @Override
    public void afterExecute() {
        WebAutomationModuleDataObject dataObj =
                (WebAutomationModuleDataObject) m_service.getDataObj();
        m_result.setReturnValueList(dataObj.getUiObjectValueList());
        super.afterExecute();
        try {
            WebAutomationModuleProviderService webAutomationService =
                    (WebAutomationModuleProviderService) m_service;
            webAutomationService.stopAutomation();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
